// pages/editCard/editCard.js 
const innerAudioContext = wx.createInnerAudioContext();  //创建音频对象
const app = getApp()
var recorderNum;
Page({
  /**
   * 页面的初始数据
   */ 
  data: {
    avatar:'',
    nickName:'',
    voice:'1',
    hasmusic:false,
    changelogo: false,
    hasluyin:false,
    greet:'',
    scend:60,
    edit:false,
  },
  greetinput: function (e) {
    var greet = this.data.greet;
    if (e.detail.value.length > 30 && e.detail.value.length > greet.length){
      wx.showToast({
        title: '祝福语不可超过30字',
        icon: 'none'
      })
      this.setData({
        greet: greet
      })
      return;
    }
    this.setData({
      greet: e.detail.value
    })
    var greet = wx.getStorageSync('greet');
    if(greet){
      greet.content = e.detail.value;
      wx.setStorageSync('greet', greet)
    }
  },
  nameinput: function (e) {
    this.setData({
      nickName: e.detail.value
    })
  },
  makecard: function () {
    var that = this
    var data = wx.getStorageSync('requestdata');
    wx.request({
      url: app.siteinfo.rootpath + 'createcard',
      data: data,
      method:'post',
      success: function (res) {
        if (res.data.code == 200) {
          wx.removeStorageSync('music')
          wx.removeStorageSync('greet')
          wx.removeStorageSync('requestdata')
          wx.redirectTo({
            url: '/pages/preview/preview?id=' + res.data.id,
          })

        } else if(res.data.code == 505) {
          wx.showToast({
            title: '祝福语或落款含有敏感字符',
            icon: "none"
          })
        } else {
          wx.showToast({
            title: '贺卡生成失败',
            icon: "none"
          })
        }
      }
    })
    // console.log(data)
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let that = this
    var id = options.id
    var userinfo = wx.getStorageSync('userinfo')
    that.setData({
      avatar: userinfo.logo,
      nickName: userinfo.wechat_name,
      hid: id
    })
    wx.request({
      url: app.siteinfo.rootpath + 'sysset',
      data: {},
      success: function (res) {
        that.setData({
          edit : res.data.edit ? true : false,
        })
      }
    })

  },
  
  /* ----------------------更改头像---------------------- */
  avatarHandle: function () {
    let that = this
    let avatar = that.data.avatar
    wx.chooseImage({
      count: 1,
      sizeType: ['original', 'compressed'],
      sourceType: ['album', 'camera'],
      success(res) {
        // tempFilePath可以作为img标签的src属性显示图片
        const tempFilePaths = res.tempFilePaths;
        tempFilePaths.map((v, k) => {
          wx.showLoading({
            title: '图片' + (k + 1) + '上传中',
          })
          wx.uploadFile({
            url: app.siteinfo.rootpath + 'thumbupload',
            filePath: v,
            name: 'file',
            success: function (res) {
              wx.hideLoading()
              res = res.data
              if (res) {
                that.setData({
                  avatar: res,
                })
              } else {
                wx.showToast({
                  title: '图片上传失败',
                  icon: "none"
                })
              }
            }
          })
        })
      }
    })
  },
  /* ----------------------选择背景音乐---------------------- */
  changeMusic: function () {
    wx.navigateTo({
      url: '/pages/music/music',
    })
  },
  /* ----------------------选择祝福语---------------------- */
  greetingHandle: function () {
    wx.navigateTo({
      url: '/pages/greeting/greeting',
    })
  },
  /* ----------------------保存---------------------- */
  saveHandle: function () {
    var that = this;
    var avatar = that.data.avatar;
    var hid = that.data.hid;
    var name = that.data.nickName;
    var greet = that.data.greet;

    var music = wx.getStorageSync('music');
    var chosegreet = wx.getStorageSync('greet');
    if (music){
      var musicpath = music.content
      var musicid = music.id
    }else{
      wx.showToast({
        title: '请选择背景音乐',
        icon: 'none'
      })
      return;
    }
    if (chosegreet) {
      var greetid = chosegreet.id
    } else {
      var greetid = 0
    }
    
    if (greet.length < 1) {
      wx.showToast({
        title: '请填写祝福语',
        icon: 'none'
      })
      return;
    }
    var requestdata = {
      id: wx.getStorageSync('user_id'),
      hid:hid,
      logo: avatar,
      name: name,
      musicpath: musicpath,
      musicid: musicid,
      greet: greet,
      greetid: greetid,
    }
    wx.setStorageSync('requestdata', requestdata)
    that.makecard()
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var that = this;
    var music = wx.getStorageSync('music')
    var greet = wx.getStorageSync('greet')
    if(music){
      that.setData({
        musicname:music.title,
        hasmusic:true
      })
    }
    if (greet) {
      var content = greet.content.split("@#$");
      content = content.join(',');
      that.setData({
        greet: content,
      })
    }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
    innerAudioContext.stop() // 关闭当前播放音频
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },
})